/*
*  Impp - Instant Messenger
*
*  Copyright (C) 2008  Oliver Schneider
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#ifndef CONTACTLIST_H_
#define CONTACTLIST_H_

#include <wx/wx.h>

#include "../../core/interfaces/iBuddyList.h"
#include "../../core/interfaces/iUserInterface.h"
#include "../../core/Group.h"
#include "BuddyTreeList.h"
#include "SearchField.h"
#include "../../core/database/DBModule.h"
#include "../../core/events/StatusEvent.h"
#include "../../core/events/AvatarChangedEvent.h"
#include "../../core/events/MessageEvent.h"
#include "../../core/events/MessageReadEvent.h"
#include "../../core/events/SelfChangedEvent.h"
#include "../../core/events/SettingChangedEvent.h"
#include "../../core/events/ImppCommandEvent.h"
#include "../../core/utils/ButtonList.h"
#include "ActionPanel.h"



namespace Impp {

class SearchField;
class ActionPanel;

enum {
		ID_CON = wxID_HIGHEST,
		ID_BUDDYLIST,
		ID_TREELIST,
		ID_SEARCH,

		ID_TOOL_MAINMENU,

		ID_STATUS_ONLINE  = wxID_HIGHEST+20,
		ID_STATUS_FREE4CHAT,
		ID_STATUS_AWAY,
		ID_STATUS_OCCUPIED,
		ID_STATUS_DND,
		ID_STATUS_NA,
		ID_STATUS_INVISIBLE,
		ID_STATUS_VISIBLE,
		ID_STATUS_OFFLINE,

		ID_ICON_STATUS,
		ID_TEXT_STATUS,
		ID_TEXT_STATUS_MSG,
		ID_STATUS_MESSAGE,
		ID_STATUS_EXTENDED_START = wxID_HIGHEST+50,
		ID_STATUS_EXTENDED_END = ID_STATUS_EXTENDED_START+30
};


#define BUDDYLIST_DB_POSITION_X							wxT("x")
#define BUDDYLIST_DB_POSITION_Y							wxT("y")
#define BUDDYLIST_DB_SIZE_WIDTH							wxT("width")
#define BUDDYLIST_DB_SIZE_HEIGHT						wxT("height")
#define BUDDYLIST_DB_BUDDY_SHOW_OFFLINE 				wxT("showOffline")
#define BUDDYLIST_DB_BUDDY_SHOW_AVAS					wxT("showAvas")
#define BUDDYLIST_DB_BUDDY_SHOWN_PRIORITY 				wxT("shownPriority")
#define BUDDYLIST_DB_BUDDY_DIM_IDLE 					wxT("dimIdle")
#define BUDDYLIST_DB_BUDDY_TREAT_UNKNOWN_AS_OFFLINE		wxT("treatUnknownAsOffline")

#define BUDDYLIST_DB_GROUPS_USE_GROUPS					wxT("useGroups")
#define BUDDYLIST_DB_GROUPS_HIDE_EMPTY					wxT("hideEmptyGroups")

#define STD_BUDDYLIST_MODULE_NAME 						wxT("StdBuddyList")
#define STD_BUDDYLIST_MODULE_SUBMENU_GROUPS				wxT("groupsSubMenu")
#define STD_BUDDYLIST_MODULE_MENU_GROUPS_USE_GROUPS		wxT("userGroups")
#define STD_BUDDYLIST_MODULE_MENU_GROUPS_HIDE_EMPTY		wxT("hideEmptyGroups")


class BuddyList : public wxFrame
{

private:
	enum{
		ID_BUDDYLIST_BUDDY_SHOW_OFFLINE,
		ID_BUDDYLIST_BUDDY_SHOWAVAS,
		ID_BUDDYLIST_BUDDY_DIMIDLE,

		ID_BUDDYLIST_BUDDY_PRIORITY_CHANGE,
		ID_BUDDYLIST_BUDDY_PRIORITY_ALL,
		ID_BUDDYLIST_BUDDY_PRIORITY_JUST_HIGH,
		ID_BUDDYLIST_BUDDY_PRIORITY_NOT_LOW,

		ID_BUDDYLIST_GROUPS_USE,
		ID_BUDDYLIST_GROUPS_HIDE_EMPTY,

		ID_BUDDYLIST_HIGHEST
	};
private:
	ButtonList buttonList;
	BuddyTreeList* list;
	SearchField* searchField;
	wxBoxSizer* sizer;
	ActionPanel* actionPanel;

	DBModule dbModule;

public:
	BuddyList();
	virtual ~BuddyList();

	void moduleInit();
	void moduleDestroy();

	wxWindow* getWindow();

	void onMessageIn(MessageEvent & event);
	void onMessageRead(MessageReadEvent & event);

	void onMainMenu(wxCommandEvent & event);
	void onCheckTool(wxCommandEvent & event);

	void updateBuddyCustomName(Buddy* buddy);

	void onShowBuddiesWithPriority(wxCommandEvent & event);
	void onClosed(wxCloseEvent & event);

	void onSettingChanged(SettingChangedEvent & event);
	void onImppAction(ImppCommandEvent & event);

	void savePosition();

	void changeHideEmptyGroups(bool hide);
	void changeUseGroups(bool useGroups);
	void changeShowOffline(bool showOffline);
	void changeShowAvas(bool showAvas);
	void changeDimIdle(bool dimIdle);

	void onKeyDown(wxKeyEvent & event);
	SearchField* getSearchField();
	BuddyTreeList* getTreeList();
	void hideSearchField();
	void layoutMainSizer();

	void addCheckSettingButton(DBModule & dbModule, const wxString & settingName, const wxString & title, const wxString & description);

private:
	void setUnreadMsg(Buddy* buddy, bool unreadMsgs);
	static void wxCALLBACK addBuddyListMainMenuEntries(iMenu* menu, void* userData);

private:
    DECLARE_EVENT_TABLE()
};

class StdBuddyListModule : public iBuddyList{

DECLARE_DYNAMIC_CLASS(StdBuddyListModule)

private:
	BuddyList* list;

public:
	StdBuddyListModule() :
		list(NULL){};

	virtual ~StdBuddyListModule(){};

	void moduleInit(){
		list = new BuddyList();
		list->moduleInit();
	}
	void moduleDestroy(){
		list->moduleDestroy();
	}

	virtual wxWindow* getWindow(){
		wxASSERT(list != NULL);
		return list;
	}

};

}

#endif /*CONTACTLIST_H_*/
